import { Query } from '@/utils/Query.js'
import appStore from '@/store/index.js'

export default function (map) {
  var list = [
    { where: '车流量<1000', color: import.meta.env.VITE_APP_BASE_GREEN },
    { where: '车流量>1500', color: import.meta.env.VITE_APP_BASE_RED },
    {
      where: '车流量>=1000 and 车流量<=1500',
      color: import.meta.env.VITE_APP_BASE_YELLOW,
    },
  ]
  const store = appStore

  /* 获取input的值 */

  function queryLoad() {
    store.showStore.handleShowTure()

    list.map((item) => {
      let where = item.where
      const service = {
        docName: 'smartRoad',
        layerId: 1,
      }
      Query.queryBySql({
        where,
        service,
        callback: (res) => querySuccess(res, item),
      })
    })
  }

  function querySuccess(result, item) {
    var format = new Zondy.Format.PolygonJSON()
    //将MapGIS要素JSON反序列化为ol.Feature类型数组
    var features = format.read(result)

    //实例化一个矢量图层drawLayerr用于高亮显示结果
    var drawSource = new ol.source.Vector({
      wrapX: false,
    })
    drawSource.addFeatures(features)
    // 设置样式
    let drawLayer = new ol.layer.Vector({
      source: drawSource,
      style: new ol.style.Style({
        //填充色
        fill: new ol.style.Fill({
          color: item.color,
        }),
        //边线样式
        stroke: new ol.style.Stroke({
          color: item.color,
          width: 5,
        }),
      }),
    })
    map.value.addLayer(drawLayer)
  }

  function delQueryLoad() {
    store.showStore.handleShowFalse()
    map.value.getLayers().array_.splice(3, 3)
    map.value.render()
  }

  return { queryLoad, delQueryLoad }
}
